//
//  MyLabelDownView.m
//  FD
//
//  Created by xiaofang wang on 2018/12/30.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "MyLabelDownView.h"

#import "DCCollectionHeaderLayout.h"
#import "MyLabelModel.h"
#import "XJFeatureItem.h"
#import "XJFeatureSectionHeaderView.h"
static NSString *const XJFeatureItemReuseId = @"XJFeatureItemReuseId";


@interface MyLabelDownView()<UICollectionViewDelegate,
UICollectionViewDataSource,
HorizontalCollectionLayoutDelegate
>
/**1.10px线*/
@property (nonatomic, strong) UILabel *lineView;
/**2.所有标签*/
@property (nonatomic, strong) UILabel *allTagLb;
/**3.collectionveiw*/
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation MyLabelDownView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    //线
    [self addSubview:self.lineView];
    
    //所有标签:
    [self addSubview:self.allTagLb];
    
    //collectionview
    [self addSubview:self.collectionView];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //线
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.height.mas_equalTo(10);
    }];
    
    //label
    [self.allTagLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.top.equalTo(self.lineView.mas_bottom);
        make.height.mas_equalTo(30);
        make.width.mas_greaterThanOrEqualTo(100); //2019-2-23高度自适应
    }];

    //3.collectionview
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.allTagLb.mas_bottom);
        make.left.right.bottom.equalTo(self);
    }];
}


#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.allTagArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    XJFeatureItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:XJFeatureItemReuseId forIndexPath:indexPath];

    MyLabelModel *model = self.allTagArr[indexPath.row];
    item.itemModel = model;
    
    return item;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //取出model
    MyLabelModel *model = self.allTagArr[indexPath.row];
    
    //回调出去
    if (self.downViewClickItemBlock) {
        self.downViewClickItemBlock(model);
    }
}

#pragma mark - HorizontalCollectionLayoutDelegate
// item宽度自适应
- (NSString *)collectionViewItemSizeWithIndexPath:(NSIndexPath *)indexPath {
    
    //    return self.dataArr[indexPath.section].items[indexPath.row].infoname;
    
    MyLabelModel *model = self.allTagArr[indexPath.row];
//    FDLog(@"**************:%@",model);
    return model.name;
}

#pragma mark - 接口
//刷新数据
- (void)downViewLoadData {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.collectionView reloadData];
    });
}

#pragma mark - getter

- (UILabel *)lineView {
    if (!_lineView) {
        _lineView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        _lineView.backgroundColor = kMainLightGrayColor;
    }
    return _lineView;
}
- (UILabel *)allTagLb {
    if (!_allTagLb) {
        _allTagLb = [[UILabel alloc] init];
        _allTagLb.text = NSLocalizedString(AllTags, @"所有标签:");
    }
    return _allTagLb;
}
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        DCCollectionHeaderLayout *layout = [DCCollectionHeaderLayout new];
        layout.lineSpacing = 8;
        layout.interitemSpacing = 20;
//        layout.headerViewHeight = 40; //12-30
        layout.delegate = self;
      
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        
        //背景色
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    
        //注册cell
        [_collectionView registerClass:[XJFeatureItem class] forCellWithReuseIdentifier:XJFeatureItemReuseId];
    }
    return _collectionView;
}
@end
